home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / more13.zip / MORE.C < prev    next >
Text File  |  1990-06-26  |  30KB  |  1,416 lines

  1. /* Special More */
  2.  
  3. #ifndef __COMPACT__
  4. #error Use COMPACT memory model
  5. #endif
  6.  
  7. #include <stdio.h>
  8. #include <io.h>
  9. #include <stdlib.h>
  10. #include <alloc.h>
  11. #include <assert.h>
  12. #include <conio.h>
  13. #include <string.h>
  14. #include <process.h>
  15. #include <dir.h>
  16.  
  17. #define MAXLINES    32000
  18. #define MAXLEN      2048
  19.  
  20. #define CASE_SEN    1
  21. #define NORMAL      2
  22. #define REPEAT      3
  23. #define REVERSE     4
  24.  
  25. #define GET        0
  26. #define SET        1
  27.  
  28. #define WRITE_FILE    0
  29. #define WRITE_BLOCK    1
  30.  
  31. #define highlight() { textcolor(col[2]);textbackground(col[5]); }
  32. #define inverse()   { textcolor(col[1]);textbackground(col[4]); }
  33. #define normal()    { textcolor(col[0]);textbackground(col[3]); }
  34. #define flashing()  { textcolor(col[2] + BLINK);textbackground(col[5]); }
  35.  
  36. #define rest_keys() gotoxy(1,25);inverse();cprintf(keys);clreol();normal()
  37. #define rest_message() gotoxy(1,1);inverse();cprintf(message);clreol();normal()
  38.  
  39. #define open_con_w() freopen("con","wt",stdout); setbuf(stdout, NULL)
  40.  
  41. #define ESC         27
  42. #define UPARROW     72+256
  43. #define DOWNARROW   80+256
  44. #define LEFTARROW   75+256
  45. #define RIGHTARROW  77+256
  46. #define HOME        71+256
  47. #define END         79+256
  48. #define PGUP        73+256
  49. #define PGDN        81+256
  50. #define CTRLPGUP    132+256
  51. #define CTRLPGDN    118+256
  52. #define CTRLRIGHT    115+256
  53. #define F1          59+256
  54. #define F7          65+256
  55. #define F8          66+256
  56. #define F9          67+256
  57. #define F10         68+256
  58.  
  59. void read_file(void);
  60. void display(void);
  61. void putseg(int loc, int line, int start);
  62. void disp_page(int column, int line);
  63. void open_kbd(void);
  64. void open_file(void);
  65. int  open_prn(void);
  66. int  getscan(void);
  67. char *_cgets(char *s);
  68. char getline(char *s);
  69. void strseg(char *dest, char *src, int start, int len);
  70. int  find(int type, int line);
  71. void help(int line, int column);
  72. void print(int line, int column);
  73. void print_all(void);
  74.  
  75. void colors(int column, int line);
  76. void printer(int column, int line);
  77. void editor(int column, int line);
  78. void read_setup(void);
  79. void write_setup(void);
  80.  
  81. int  bookmark(int cmd, int line);
  82. int  goto_line(int line);
  83.  
  84. void read_name(void);
  85.  
  86. void write_file(int func);
  87. void edit(void);
  88.  
  89. int  tot_lines;
  90. char filename[129];
  91. char prg_path[129];
  92. char message_str[66];
  93. char file_r;
  94. int  block_begin = -32767;
  95. int  block_end = -32767;
  96.  
  97. struct str_struct *ptrs[(MAXLINES/32)+1];
  98.  
  99. int col[6] = { LIGHTGRAY, BLACK, WHITE, BLACK, LIGHTGRAY, BLACK };
  100.  
  101. #define NOSTOR "%s: insufficent memory for file"
  102. #define FNDERR "%s: structure managament error"
  103.  
  104. int cr = 0;
  105.  
  106. #define chkptr(x) if( x == NULL ) { clrscr(); printf(NOSTOR,name); exit(2); }
  107. #define chkfail() if( find_fail ) { clrscr(); printf(FNDERR,name); exit(2); }
  108. #define str_size sizeof(struct str_struct)
  109.  
  110. struct str_struct {
  111.     int rec_num;
  112.     long pos;
  113.     char *str[32];
  114. };
  115.  
  116. char *ptr(int line);
  117. void setptr(int line, char *s);
  118.  
  119. int bookmarks[10] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
  120.  
  121. char port[68] = "prn";
  122. char edit_cmd[68] = "edit @";
  123.  
  124. char prg_name[128];
  125. char name[9];
  126.  
  127. char *keys = "Keys: cursor, Find Case Next Back, Print All, block: +-*, Get Set, Line F1=Help";
  128. char *message = "Enhanced More by Daniel Sachs. Copyright 1990 by Daniel Sachs.";
  129. char *date = __DATE__;
  130.  
  131. void main(int argc, char *argv[])
  132. {
  133.      argc--;
  134.  
  135.     open_con_w();
  136.  
  137.     strcpy(prg_name,argv[0]);
  138.     read_name();
  139.  
  140.      if (argv[0] == NULL)
  141.      {
  142.         printf("%s: DOS 3.0 or greater required",name);
  143.           exit(2);
  144.      }
  145.  
  146.      if (argc > 1)
  147.      {
  148.         printf("%s: too many arguments. syntax: %s <filename>\n\r",name,name);
  149.         printf("%s: if no file is specified, reads standard input",name);
  150.           exit(2);
  151.      }
  152.  
  153.      if (argc == 1)
  154.      {
  155.           file_r = 1;
  156.           strcpy(filename,argv[1]);
  157.           open_file();
  158.                if (strlen(filename) < 65)
  159.                {
  160.                     strcpy(message_str,filename);
  161.                     message = strupr(message_str);
  162.             }
  163.             else
  164.             {
  165.                 int i,j;
  166.  
  167.                 strcpy(message_str,"\x11\xc4\xc4");
  168.  
  169.                 j = strlen(filename)-56;
  170.  
  171.                 for(i=0;i<60;i++)
  172.                     message_str[i+3]=*(filename+j+i);
  173.  
  174.                 message_str[i] = 0;
  175.  
  176.                 message = strupr(message_str);
  177.             }
  178.      }
  179.      else
  180.         file_r = 0;
  181.  
  182.      directvideo = 1;
  183.  
  184.      read_setup();
  185.  
  186.      normal();
  187.      clrscr();
  188.  
  189.      highlight();
  190.      gotoxy(1,24);
  191.  
  192.      if (!file_r)
  193.           cputs("Reading standard input, please wait...");
  194.      else
  195.           cputs("Reading file, please wait...");
  196.  
  197.      gotoxy(23,5);
  198.     cputs("E N H A N C E D   M O R E   v1.3\n\n\n\r");
  199.      normal();
  200.      cputs("written 6/90 by Daniel Sachs, copyright 1990 by Daniel Sachs\n\r");
  201.      cputs("written in Turbo C. Portions copyright 1988 by Borland, Inc.");
  202.      cprintf("\r\n\nCompiled on %s.",date);
  203.      read_file();
  204.      display();
  205.      window(1,1,80,25);
  206.      gotoxy(1,25);
  207.      clreol();
  208.      exit(0);
  209. }
  210.  
  211. void read_name(void)
  212. {
  213.     char fdrive[3];
  214.     char fdir[65];
  215.     char fext[5];
  216.  
  217.     fnsplit(prg_name,fdrive,fdir,name,fext);
  218.     strlwr(name);
  219. }
  220.  
  221. void read_file(void)
  222. {
  223.     char s[MAXLEN],eof,*tmp;
  224.     int i,j;
  225.  
  226.      gotoxy(1,20);
  227.  
  228.      for (i=0;;i++)
  229.      {
  230.           if (i == MAXLINES)
  231.           {
  232.                clrscr();
  233.             printf("%s: file is too long",name);
  234.                exit(2);
  235.           }
  236.         eof = getline(s);
  237.  
  238.         tmp = malloc(strlen(s)+1);
  239.  
  240.         if (tmp == NULL)
  241.           {
  242.                clrscr();
  243.             printf("%s: insufficient memory for file",name);
  244.                exit(2);
  245.         }
  246.  
  247.         strcpy(tmp,s);
  248.  
  249.         setptr(i,tmp);
  250.  
  251.           if ((i % 40) == 0)
  252.           {
  253.                gotoxy(50,24);
  254.             cprintf("Line %5i. %3iK Free.",i,(int)(coreleft()/1024));
  255.                gotoxy(1,20);
  256.           }
  257.  
  258.         if (eof)
  259.         {
  260.             for(j=0;j<strlen(s);j++)
  261.                 if(s[j] != 32)
  262.                 {
  263.                     i++;
  264.                     break;
  265.                 }
  266.             break;
  267.         }
  268.      }
  269.  
  270.      tot_lines = i-1;
  271.  
  272.      for (;i < tot_lines+24;i++)
  273.         setptr(i,"");
  274. }
  275.  
  276. void read_setup(void)
  277. {
  278.      FILE *setup;
  279.  
  280.      char fdrive[3];
  281.      char fdir[65];
  282.      char fnam[9];
  283.      char fext[5];
  284.  
  285.      char name[127];
  286.  
  287.      fnsplit(prg_name, fdrive, fdir, fnam, fext);
  288.      strcpy(fext,".CFG");
  289.      fnmerge(name, fdrive, fdir, fnam, fext);
  290.  
  291.      setup = fopen(name,"rt");
  292.  
  293.      if (setup != NULL)
  294.      {
  295.           fread(edit_cmd, 1, 68, setup);
  296.           fread(port, 1, 68, setup);
  297.           fread(col, 2, 6, setup);
  298.           fclose(setup);
  299.      }
  300. }
  301.  
  302. void write_setup(void)
  303. {
  304.      FILE *setup;
  305.  
  306.      char fdrive[3];
  307.      char fdir[66];
  308.      char fnam[10];
  309.      char fext[5];
  310.  
  311.      char path[127];
  312.  
  313.      fnsplit(prg_name, fdrive, fdir, fnam, fext);
  314.      strcpy(fext,".CFG");
  315.      fnmerge(path, fdrive, fdir, fnam, fext);
  316.  
  317.      setup = fopen(path,"wb");
  318.  
  319.      if (setup != NULL)
  320.      {
  321.           fwrite(edit_cmd, 1, 68, setup);
  322.           fwrite(port, 1, 68, setup);
  323.           fwrite(col, 2, 6, setup);
  324.           fclose(setup);
  325.      }
  326.      else
  327.           putchar(7);
  328. }
  329.  
  330. void open_kbd(void)
  331. {
  332.      freopen("con","rt",stdin);
  333.      assert(stdin != NULL);
  334. }
  335.  
  336. void open_file(void)
  337. {
  338.      FILE *filetest;
  339.  
  340.      filetest=freopen(filename,"rt",stdin);
  341.      if (filetest == NULL)
  342.      {
  343.         printf("%s: unable to open file",name);
  344.           exit(4);
  345.      }
  346. }
  347.  
  348. int open_prn(void)
  349. {
  350.      FILE *filetest;
  351.  
  352.      filetest=freopen(port,"wt",stdprn);
  353.  
  354.      return(filetest != NULL);
  355. }
  356.  
  357. char getline(char *s)
  358. {
  359.      int i,j,x,eof,col;
  360.      char t[MAXLEN];
  361.      static char message = 0;
  362.  
  363.      for(i=0;;i++)
  364.      {
  365.           x = getchar();
  366.           eof = x == EOF;
  367.           if (((x != EOF) && (x != '\n')) && (i < MAXLEN-1))
  368.                t[i]=(char)x;
  369.           else
  370.                break;
  371.      }
  372.      t[i]=0;
  373.  
  374.      if ((i>=MAXLEN-1) && !message)
  375.      {
  376.           gotoxy(1,23);
  377.         cprintf("%s: lines too long, split...",name);
  378.           message=1;
  379.           gotoxy(1,20);
  380.      }
  381.  
  382.      x = 0;
  383.      col = 1;
  384.  
  385.      for (i=0;i<strlen(t);i++)
  386.      {
  387.           if (t[i]!='\t')
  388.           {
  389.                s[x++]=t[i];
  390.                col++;
  391.           }
  392.           els